var express = require('express');
var router = express.Router();
var shortId = require('shortid')
const QuestModel = require("../db/questModel");

module.exports = (req, res) => {
    const id = req.params["id"]
    const data = req.body
    const date = Date.now()
    data["releaseTime"] = date

    if (!data.title) {
        if (!data.status) {
            if (data.endTime === null) {
                QuestModel.updateOne(
                    {"id": parseInt(id), "username": req.user.username},
                    {$set: {"endTime": null}},
                    function (error, ret) {
                        if (error) {
                            res.send({
                                "code": 0,
                                "openId": null
                            })

                        } else {
                            res.send({
                                "code": 1,
                                "openId": "修改成功"
                            })
                        }
                    })
            }
            else{
                QuestModel.updateOne(
                    {"id": parseInt(id), "username": req.user.username},
                    {$set: {"endTime": data.endTime,"lastEditedTime": date}},
                    function (error, ret) {
                        if (error) {
                            res.send({
                                "code": 0,
                                "openId": null
                            })

                        } else {
                            res.send({
                                "code": 1,
                                "openId": "修改成功"
                            })
                        }
                    })
            }
        }
        else{
            shortId.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#^')
            const sid = shortId.generate()
            const str = sid.toString()
            data["openCode"] = str

            if (data.status === 2 || data.status === "2")
                QuestModel.updateOne(
                    {"id": parseInt(id), "username": req.user.username},
                    {$set: {"openCode": str, "releaseTime": date, "lastEditedTime": date, "status": 2}},
                    function (error, ret) {
                        if (error) {
                            res.send({
                                "code": 0,
                                "openId": null
                            })

                        } else {
                            res.send({
                                "code": 1,
                                "openId": sid
                            })
                        }
                    })
            else
                QuestModel.updateOne({"id": parseInt(id)},
                    {$set: {"releaseTime": null, "status": 1, "openCode": null, "lastEditedTime": date}},
                    function (error, ret) {
                        if (error) {
                            res.send({
                                "code": 0,
                                "msg": error
                            })
                        } else {
                            res.send({
                                "code": 1,
                                "msg": "修改成功"
                            })
                        }
                    })
        }
}
    else{
        req.body["lastEditedTime"] = Date.now()
        QuestModel.updateOne({"id":parseInt(id)},{$set:req.body},function(error,ret){
            if (!error) {
                res.send({
                    "code": 1,
                    "msg": "修改成功"
                })
            } else {
                res.send({
                    "code": 0,
                    "msg": error
                })
            }
        })
    }
}
